<?php
/**
 * 本页面用于单个采集
 * 仅供测试用 
 */
	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$tid = $_GET['tid'];
	
	if(!is_numeric($tid) || $tid < 0){
		show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
	}
	//从TID获取文章列表连接
	$indexpre = 'http://www.xiaoshuo520.com/Book/';
	$indexadd =  '/Index.aspx';
	$indexurl = $indexpre . $tid . $indexadd;
	$c = file_get_contents($indexurl);
	$c = explode('<img src="/skin/default/images/yd_ico.gif" width="22" height="22" align="absmiddle">'
	,$c);
	$c = explode('<strong><a href="',$c[1]);
	$c = explode('"><',$c[1]);
	$c = $c[0];
	$base_url = 'http://www.xiaoshuo520.com';
	$url =  $base_url . $c;
	$tid = explode('/',$url);
	$eid = $tid[count($tid) - 3];
	$tid = $tid[count($tid) - 2];
	//分离URL以及ID
	$id = $tid;
	//定义公用变量
	
	
		
	//获取URL内容
	$content = 	file_get_contents($url);
	$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
	
	//获取名称
	$name = explode('</title>',$content);
	$name = $name[0];
	$name = explode('<title>',$name);
	$name = $name[1];
	$name = explode('最新章节列表',$name);
	$name = $name[0];
	
	//获取最后更新时间
	$dt = explode('</b>于',$content);
	$dt = $dt[1];
	$dt = explode('由',$dt);
	$dt = strtotime(trim($dt[0]));
	
	//获取URL列表
	$urls = explode('<div id="BookText"><br />',$content);
	$urls = $urls[1];
	$urls = explode('<div id="End">',$urls);
	$urls = $urls[0];
	$replacer = array('<div id="ListEnd"></div>','<li>','</li>');
	$replacerc = array('','',"\r\n");
	$urls = str_replace($replacer,$replacerc,$urls);
	$match_str = "/<a href=\"(.*)\" title=\"(.*)\">/isU";
	preg_match_all($match_str,$urls,$urln);
	//获取每章连接以及章节标题和更新时间
	$urlns = $urln[1];
	$titlens = $urln[2];
	
	unset($urln);
	//获取每单位总数
	$ulnum = count($urlns);
	//进入每页进行处理
	//插入分类信息
	//检查当前ID是否存在
	$where = 'AND id= ' . $id .'';
	$result = $DB->build_and_exec_query(
		array('select' => 'COUNT(*) as max','from'   => '520tags','where'	=> "1 " . $where ,)
	);
	//当已经存在的时候更新日期
	if ($result['max'] > 0) {
		
	}
	//当不存在时插入
	else{
		do_taginsert($id,$eid,$dt,$name,0);
	}
	
	
	
	//获取数据库COUNT
	$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'count','from'   => '520tags','where'	=> "1 " . $where ,)
		);	
	$dbcount = $result['count'];
	
	foreach ($urlns as $k=>$v) {
		//当COUNT小于已经采集的时候，跳过。
		if ($k < $dbcount) {
			continue;
		}
		//老版直接采集IF
		$comic_contents = mb_convert_encoding(file_get_contents($url.$v),"UTF-8","GB2312");
		//从IF中获取连接
		$dtd = explode('border:0;" src="',$comic_contents);
		$dtd = explode('" height="100%',$dtd[1]);
//		echo $dtd[0];exit;
		
		//新版的方法
//		$v = str_replace('.shtml','',$v);
//		$current_url = $base_url . '/Html/ShowChapter.aspx?cid=' . $v . '&bid=' . $tid;
//		$comic_contents = mb_convert_encoding(file_get_contents($current_url),"UTF-8","GB2312");
//		//由于改版，需要进一步获取连接
//		//获取最新位置
//		$dtd = explode('<td>&nbsp;&nbsp;<a href="',$comic_contents);
//		$dtd = explode('" target="_parent">',$dtd[1]);
		
		
		$comic_contents = mb_convert_encoding(file_get_contents($dtd[0]),"UTF-8","GB2312");
		if ($comic_contents == '') {
			//注意如果这里出错了，则该页将无法采集。
			continue;
		}
		//获取文章内容，这部分涉及到判断是否为图片，我的意见是直接将图片存成连接文字。
		//确定处理范围
		$comic_contents = explode('<div id="BookText"><br />&nbsp;&nbsp;&nbsp;&nbsp;',$comic_contents);
		$comic_contents = explode("<script type='text/javascript' src='http://drmcmm.baidu.com/js/s.js'></script>",$comic_contents[1]);
		//替换关键字定义
		$replacer = array(' ',"<div style='display:none'>",'&nbsp;','</div>',"<divstyle='display:none'>",'言情言情言情言情言情','文学文学文学文学文学','书库书库书库书库书库','网络文学网络文学网络文学网络文学网络文学','武侠武侠武侠武侠武侠','网游网游网游网游网游','玄幻玄幻玄幻玄幻玄幻','奇幻奇幻奇幻奇幻奇幻','杂志杂志杂志杂志杂志','小说小说小说小说小说','散文散文散文散文散文','诗歌诗歌诗歌诗歌诗歌','杂谈杂谈杂谈杂谈杂谈');
		$replacerc = array('');
		$text = str_replace($replacer,$replacerc,$comic_contents[0]);
		$replacer = array('<br>','<br/><br/>','<br/>');
		$replacerc = array('<br/>','<br/>',"\r\n");
		$text = str_replace($replacer,$replacerc,$text);
		$text = htmlspecialchars($text);
		//判断图像
		$ispic = 0;
		if (strstr($text,'img')) {
			$ispic = 1;
		}
		//获取章节名称
		$title = $titlens[$k];
		$title = explode('更新时间',$title);
		$title = explode('章节名:',$title[0]);
		
		//这里添加数据库插入语句
		do_photoinsert($tid,$dtd[0],$text,$title[1],0,$ispic);
		echo '<font color="green">已成功添加章节：'. $title[1] . '</font><BR>';
	}
	
	//更新计数
	$where = 'id= ' . $id .'';
	$DB->do_update('520tags',
		array(					  
		   'lastupdate'				=> $dt,
		   'count'					=> count($urlns),
		 ),
		 $where
	);
	
		
	
	
	function do_taginsert($id,$eid,$lu,$name,$count){
		global $DB;
		$DB->do_insert( '520tags', array( 'id'			=> $id,
								   'eid'					=> $eid,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前小说','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$text,$title,$count,$ispic){
		global $DB;
		$DB->do_insert( '520photos', array( 
								   'uid'				=> $uid,
								   'url'					=> $url,
								   'ispic'					=> $ispic,
								   'name'					=> $title,
								   'text'					=> $text,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前小说','添加出错','添加');
		}
	}
	
	echo "采集完毕";
	
	
	exit;
	
	
?>